﻿package com.controller 
{
	import com.adobe.serialization.json.JSON;
	import com.model.PicListProxy;
	import com.model.vo.PicVo;
	import com.utils.DebugTrace;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	
	/**
	 * startup完成之后，读取txt文件，存储数据
	 * @author Mr.zheng
	 */
	public class OverStartupCommand extends SimpleCommand implements ICommand
	{
		public static const DATACOMPLETE:String = "overStatrtCommand_dataComplete";
		private var urlLoader:URLLoader;
		public function OverStartupCommand() 
		{
			urlLoader = new URLLoader();
		}
		override public function execute(notification:INotification):void 
		{
			super.execute(notification);
			var urlStr:String = notification.getBody() as String;
			urlLoader.load(new URLRequest(urlStr));
			urlLoader.addEventListener(Event.COMPLETE, onLoadComplete);
		}
		
		private function onLoadComplete(e:Event):void 
		{
			var picList:Array = parse(JSON.decode(e.target.data));
			var picListProxy:PicListProxy = facade.retrieveProxy(PicListProxy.NAME) as PicListProxy;
			var obj:Object={};
			obj.list=picList;
			obj.currentNum=0;//设置默认显示图片的编号
			picListProxy.setData(obj);
			
			DebugTrace.objTrace(picListProxy.getData());
			sendNotification(DATACOMPLETE);
			
		}
		/**
		 *解析数据 
		 * @param arr
		 * @return 
		 * 
		 */		
		private function parse(arr:Array):Array {
			var parseArr:Array = new Array();
			for(var i:* in arr){
				var picVo:PicVo = new PicVo();
				picVo.title = arr[i].picTitle;
				picVo.describe = arr[i].picDescribe;
				picVo.url = arr[i].picUrl;
				parseArr.push(picVo);
			}
			return parseArr;
		}
		
	}

}